package pl.edu.agh.winmon.hibernate;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * Represents information collecting turn.
 */
@Entity
@Table(name = "`" + Turn.TABLE_NAME + "`")
public class Turn implements Serializable {

	@Transient
	private static final long serialVersionUID = -7058840217626021134L;

	/**
	 * Table name.
	 */
	@Transient
	public static final String TABLE_NAME = "turn"; //$NON-NLS-1$

	/**
	 * Hibernate's annotation property: id.
	 */
	@Transient
	public static final String PROP_ID = "id"; //$NON-NLS-1$

	/**
	 * Database column name: id;
	 */
	@Transient
	public static final String DB_ID = PROP_ID;

	@Transient
	public static final String DB_MEMORY_STAT_INFO = "memory_stat_entry_id"; //$NON-NLS-1$

	/**
	 * Hibernate's annotation property: session id.
	 */
	@Transient
	public static final String PROP_SESSION = "session"; //$NON-NLS-1$

	/**
	 * Database column name: session id;
	 */
	@Transient
	public static final String DB_SESSION = "session_id"; //$NON-NLS-1$

	/**
	 * Hibernate's annotation property: start time.
	 */
	@Transient
	public static final String PROP_START_TIME = "startTime"; //$NON-NLS-1$

	/**
	 * Database column name: start time;
	 */
	@Transient
	public static final String DB_START_TIME = "start_time"; //$NON-NLS-1$

	/**
	 * Hibernate's annotation property: CPU loads.
	 */
	@Transient
	public static final String PROP_CPU_LOADS = "cpusLoads"; //$NON-NLS-1$

	/**
	 * Hibernate's annotation property: processes.
	 */
	@Transient
	public static final String PROP_PROCESSES = "processesStatEntry"; //$NON-NLS-1$

	@Transient
	public static final String PROP_MEMORY = "memoryStatEntry"; //$NON-NLS-1$

	/**
	 * Id of the turn.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = DB_ID)
	private Long id;

	/**
	 * Session related to this turn.
	 */
	@ManyToOne
	@JoinColumn(name = DB_SESSION)
	private Session session;

	/**
	 * Time when this turn has been started.
	 */
	@Column(name = DB_START_TIME)
	private Long startTime;

	@OneToMany(mappedBy = TABLE_NAME)
	@OrderColumn(name = CpuLoad.PROP_ID)
	private List<CpuLoad> cpusLoads;

	/**
	 * Processes related to this turn.
	 */
	@OneToMany(mappedBy = TABLE_NAME)
	@OrderColumn(name = ProcessStatEntry.PROP_ID)
	private List<ProcessStatEntry> processesStatEntry;

	@OneToOne(targetEntity = MemoryStatEntry.class)
	@JoinColumn(name = DB_MEMORY_STAT_INFO)
	private MemoryStatEntry memoryStatEntry;

	/**
	 * Returns turn's id.
	 * 
	 * @return Turn's id.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * Sets turn's id.
	 * 
	 * @param id
	 *            Turn's id.
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Returns session related to this turn.
	 * 
	 * @return Session related to this turn.
	 */
	public Session getSession() {
		return session;
	}

	/**
	 * Sets session related to this turn.
	 * 
	 * @param session
	 *            Session related to this turn.
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	/**
	 * Returns time (in long format) when turn has been started.
	 * 
	 * @return Time (in long format) when turn has been started.
	 */
	public Long getStartTime() {
		return startTime;
	}

	/**
	 * Sets time (in long format) when turn has been started.
	 * 
	 * @param startTime
	 *            Time (in long format) when turn has been started.
	 */
	public void setStartTime(Long startTime) {
		this.startTime = startTime;
	}

	/**
	 * Returns load of CPUs.
	 * 
	 * @return Load of CPUs.
	 */
	public List<CpuLoad> getCpusLoads() {
		return cpusLoads;
	}

	/**
	 * Sets load of CPUs.
	 * 
	 * @param cpuLoads
	 *            Load of CPUs.
	 */
	public void setCpusLoads(List<CpuLoad> cpusLoads) {
		this.cpusLoads = cpusLoads;
	}

	public List<ProcessStatEntry> getProcessesStatEntry() {
		return processesStatEntry;
	}

	public void setProcessesStatEntry(List<ProcessStatEntry> processesStatEntry) {
		this.processesStatEntry = processesStatEntry;
	}

	public MemoryStatEntry getMemoryStatEntry() {
		return memoryStatEntry;
	}

	public void setMemoryStatEntry(MemoryStatEntry memoryStatEntry) {
		this.memoryStatEntry = memoryStatEntry;
	}

}
